// import { modifyData } from "@/utils";
import config from "@/utils/config";
import { reactive } from "vue";

/** vite挂载到import.meta的环境数据 */
const viteEnv: {
  BASE_URL: string;
  DEV: boolean;
  MODE: "development" | "production";
  PROD: boolean;
  SST: boolean;
} = (import.meta as any).env;

/** 不同环境url包含的字符串, 用于判断当前是测试环境还是正式环境, 要求不能相互匹配!!! */
const urlSubStringInfo = {
  prod: "mypity.lammu.cn",
  test: "mypitytest.lammu.cn",
};

/**
 * 子应用名称对应的基础url
 */
const subAppName2baseUrlInfo_dev = {
  appA: "http://localhost:2333/appA",
  appB: "http://localhost:3000/appB",
};
const subAppName2baseUrlInfo_test = {
  appA: "https://mypitytest.lammu.cn/appA",
  appB: "https://mypitytest.lammu.cn/appB",
};
const subAppName2baseUrlInfo_prod = {
  appA: "https://mypity.lammu.cn/appA",
  appB: "https://mypity.lammu.cn/appB",
};

function getDefaultConfig() {
  return {};
}

export default class StoreConfig {
  /** 登录后统一设置的数据 */
  readonly info = reactive(getDefaultConfig());
  /** 存储在cookie的token 键名 */
  readonly tokenKey = "login_admin_ticket";
  /** 请求超时时间 ms */
  readonly requestOvertime = 10 * 1000;
  /** 请求代理地址(测试环境地址) */
  readonly proxyTargetUrl = config.proxyTargetUrl;
  /** 子应用名称对应的基础url */
  readonly subAppName2baseUrlInfo = {
    dev: subAppName2baseUrlInfo_dev,
    test: subAppName2baseUrlInfo_test,
    prod: subAppName2baseUrlInfo_prod,
  }[this.env];
  /** 开发端口 */
  readonly devPort = config.devPort;
  /** 是否开发环境 */
  readonly isDev = viteEnv.DEV;
  /** 是否测试环境 */
  readonly isTest = this.env === "test";
  /** 是否生产环境 */
  readonly isProd = this.env === "prod";
  /** hostname */
  readonly hostname = process.client ? window?.location?.hostname : '';
  /** 外链列表 */
  get outsideUrlInfo() {
    return {
      login: `https://control.ifuhua.com.cn/${this.isProd ? "kjdatalogin" : "datalogin"}/#/?backUrl=${process.client ? window.location.href:''}`,
    };
  }
  /** 运行环境 */
  get env(): "dev" | "test" | "prod" {
    const hostname = process.client ? window?.location.hostname : '';
    if (viteEnv.DEV) {
      return "dev";
    } else if (hostname.includes(urlSubStringInfo["test"])) {
      return "test";
    } else if (hostname.includes(urlSubStringInfo["prod"])) {
      return "prod";
    } else {
      console.error("判断不出当前代码运行环境,请检查配置!!!");
      return "dev";
    }
  }
  updateInfo(value: { [key: string]: any }) {
    Utils.modifyData(this.info, value);
  }
  resetInfo() {
    Utils.modifyData(this.info, getDefaultConfig());
  }
}
